package com.common.python;
import com.common.utils.UploadConfig;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.util.Scanner;

public class ToolTest {
    public static void main(String [] args) {
        try {
            /*String str = String.format("C:\\Users\\tm\\AppData\\Local\\Programs\\Python\\Python38\\python C:\\Users\\tm\\AppData\\Local\\Programs\\Python\\Python38\\project\\gps\\gps_calc.py %s","--d 20230810");
            System.out.println(str);
            Process proc =  Runtime.getRuntime().exec(str);//执行脚本

            BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while((line = reader.readLine()) != null){
                System.out.println(line);
            }
            reader.close();
            proc.waitFor();*/

            String str = "C:\\Users\\tm\\Desktop\\20240327111739.jpg";
            System.out.println(imageBase64(str));
//            String str = "http://test-file.zhihuinl.com:8866/upload/profile//2024/02/29/2f86aed2-9df4-4f61-a40e-1a44326e2d7d.jpg";
//            int index = str.indexOf("profile");
//            String filePath = UploadConfig.getFilePath()+str.substring(index+8,str.length());
//            System.out.println(filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //将图片文件转化为字节数组字符串，并对其进行Base64编码处理
    public static String imageBase64(String imgFile) {
        //待处理的本地图片
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        //返回Base64编码过的字节数组字符串
        String s = encoder.encode(data);
        return s;
//        return "data:image/png;base64," + encoder.encode(data);
    }

}
